package kiosk;


import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
/**
 *
 * @author Ragnar
 */
public class GUISalgsListe extends JFrame {

    private SalgsListe salg;
    private SalgsTabellModel dtm;
    private JTable tbl;
    private Admin admin;

    public GUISalgsListe() throws IOException
    {
        hent();
        this.salg = admin.getSalg();
        this.initKomponenter();
    }

    private void initKomponenter()
    {
        this.setSize(760, 300);
        this.setLayout(new BorderLayout());


        String col[] = { "SalgID", "SelgerID", "Sum", "Dato", "Antall Produkter" };
        dtm = new SalgsTabellModel(this.salg, col);
        tbl = new JTable();
        tbl.setModel(dtm);

        JScrollPane jsp = new JScrollPane(tbl);
        jsp.setPreferredSize(new Dimension(760, 200));
        this.add(jsp, BorderLayout.NORTH);

        JButton btn = new JButton("Vis salg");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                int rad = tbl.getSelectedRow();
                Object o = dtm.getValueAt(rad, 0);
                int salgID = Integer.parseInt(o.toString());
                Salg s = salg.getEtSalg(salgID);
                ProduktListe prod = s.getKurv();
                try
                {
                    GUIProduktListe g = new GUIProduktListe(prod, salgID);
                }
                catch (Exception a)
                {
                    
                }
            }
        });
        this.add(btn, BorderLayout.SOUTH);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public void hent() throws IOException
    {
        try
        {
            FileInputStream input = new FileInputStream("lister.ser");
            ObjectInputStream in = new ObjectInputStream(input);
            this.admin = (Admin) in.readObject();
            input.close();
            in.close();
            JOptionPane.showMessageDialog(null, "Data hentet");
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public static void main(String[] args)
    {
        try
        {
            GUISalgsListe v = new GUISalgsListe();
        }
        catch (Exception e)
        {

        }
    }


}
